﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics;
using System.Web.UI;

namespace Lilium.Web.Mvc.AjaxControlToolkit
{
	/// <summary>
	/// Base class that provides DynamicPopulate support to multiple extenders
	/// </summary>
	[RequiredResource(typeof(DynamicPopulateExtender))]
	public class DynamicPopulateExtenderControlBase: AnimationExtenderControlBase
	{
		public DynamicPopulateExtenderControlBase(string scriptType, string elementID)
			: base(scriptType, elementID)
		{
		}


		#region public string DynamicControlID

		private string m_DynamicControlID;

		/// <summary>
		/// ID of the control to dynamically populate
		/// </summary>
		[ExtenderControlProperty]
		[ClientPropertyName("dynamicControlID")]
		[DefaultValue("")]
		//[IDReferenceProperty(typeof(WebControl))]
		[Category("Behavior")]
		[SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
		public string DynamicControlID { [DebuggerNonUserCodeAttribute] get { return m_DynamicControlID ?? string.Empty; } [DebuggerNonUserCodeAttribute] set { m_DynamicControlID = value; } }

		#endregion

		#region public string DynamicContextKey

		private string m_DynamicContextKey;

		/// <summary>
		/// An arbitrary string value to be passed to the dynamic populate web method.  For example,
		/// if the element to be populated is within a data-bound repeater, this could be the ID of
		/// the current row. 
		/// </summary>
		[ExtenderControlProperty]
		[ClientPropertyName("dynamicContextKey")]
		[DefaultValue("")]
		[Category("Behavior")]
		public string DynamicContextKey { [DebuggerNonUserCodeAttribute] get { return m_DynamicContextKey ?? string.Empty; } [DebuggerNonUserCodeAttribute] set { m_DynamicContextKey = value; } }

		#endregion

		#region public string DynamicServicePath

		private string m_DynamicServicePath;

		/// <summary>
		/// The URL of the web service to call. Leave this parameter blank and specify a
		/// ServiceMethod to call a PageMethod.  The web service should be decorated with the
		/// System.Web.Script.Services.ScriptService attribute.
		/// </summary>
		[ExtenderControlProperty]
		[ClientPropertyName("dynamicServicePath")]
		[UrlProperty]
		[TypeConverter(typeof(ServicePathConverter))]
		[Category("Behavior")]
		public string DynamicServicePath { [DebuggerNonUserCodeAttribute] get { return m_DynamicServicePath ?? string.Empty; } [DebuggerNonUserCodeAttribute] set { m_DynamicServicePath = value; } }

		/// <summary>
		/// Prevent serialization of the service path if no ServiceMethod was supplied
		/// </summary>
		/// <returns>Whether or not the service path should be serialized</returns>
		[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
		private bool ShouldSerializeServicePath()
		{
			return !string.IsNullOrEmpty(DynamicServiceMethod);
		}

		#endregion

		#region public string DynamicServiceMethod

		private string m_DynamicServiceMethod;

		/// <summary>
		/// The name of the method to call on the page or web service.
		/// </summary>
		[ExtenderControlProperty]
		[ClientPropertyName("dynamicServiceMethod")]
		[DefaultValue("")]
		[Category("Behavior")]
		public string DynamicServiceMethod { [DebuggerNonUserCodeAttribute] get { return m_DynamicServiceMethod ?? ""; } [DebuggerNonUserCodeAttribute] set { m_DynamicServiceMethod = value; } }

		#endregion

		#region public bool CacheDynamicResults

		private bool m_CacheDynamicResults;

		/// <summary>
		/// Whether the results of the dynamic population should be cached and
		/// not fetched again after the first load
		/// </summary>
		[ExtenderControlProperty]
		[ClientPropertyName("cacheDynamicResults")]
		[DefaultValue(false)]
		[Category("Behavior")]
		public bool CacheDynamicResults { [DebuggerNonUserCodeAttribute] get { return m_CacheDynamicResults; } [DebuggerNonUserCodeAttribute] set { m_CacheDynamicResults = value; } }

		#endregion
		/*
		/// <summary>
		/// Ensure the properties have been set correctly
		/// </summary>
		[SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", Justification = "Assembly is not localized")]
		public override void EnsureValid()
		{
			base.EnsureValid();
			// If any properties are set, they must all be valid
			if (!string.IsNullOrEmpty(DynamicControlID) || !string.IsNullOrEmpty(DynamicContextKey) || !string.IsNullOrEmpty(DynamicServicePath) || !string.IsNullOrEmpty(DynamicServiceMethod))
			{
				if (string.IsNullOrEmpty(DynamicControlID))
				{
					throw new ArgumentException("DynamicControlID must be set");
				}
				// DynamicContextKey optional
				// DynamicServicePath optional
				if (string.IsNullOrEmpty(DynamicServiceMethod))
				{
					throw new ArgumentException("DynamicServiceMethod must be set");
				}
			}
		}
		 */
	}
}